<?php
require_once dirname(__FILE__) . '/libraries/ServiceBase.php';
/**
 * Billing Service
 *
 */
class SDOServiceBilling extends ServiceBase
{
    # 正式环境Billing WSDL URL
    const BILLING_WSDL = 'BILLING_WSDL';
    # 测试环境Billing WSDL URL
    const PRE_BILLING_WSDL = 'PRE_BILLING_WSDL';
    const ADD_APP_EXISTS_CODE = - 10208011;
    const ADD_AREA_EXISTS_CODE = - 10208023;
    protected static $errNo = array(
        - 10208001 => "serviceId is <= 0 ", 
        - 10208004 => "spId is null ", 
        - 10208007 => "name is null ", 
        - 10208008 => "name is more than 50 chars ", 
        - 10208009 => "TimeTypeId is not in(1,2,4,5) ", 
        - 10208010 => "notes is more than 200 chars ", 
        - 10208011 => "The ServiceId is Exist ", 
        - 10208012 => "add service error ", 
        - 10208013 => "serviceId is <= 0 ", 
        - 10208016 => "areaid is < 0 ", 
        - 10208019 => "name is null ", 
        - 10208020 => "name is more than 50 chars ", 
        - 10208021 => "notes is more than 200 chars ", 
        - 10208022 => "The ServiceId is not Exist ", 
        - 10208023 => "The AreaId for the ServiceId is Exist ", 
        - 10208024 => "add area error ", 
        - 10208025 => "serviceId is <= 0 ", 
        - 10208028 => "name is null ", 
        - 10208029 => "name is more than 50 chars ", 
        - 10208030 => "TimeTypeId is not in(1,2,4,5) ", 
        - 10208031 => "notes is more than 200 chars ", 
        - 10208032 => "The ServiceId is not Exist ", 
        - 10208033 => "modify service error ", 
        - 10208034 => "serviceId is <= 0 ", 
        - 10208037 => "The ServiceId is not Exist ", 
        - 10208038 => "delete service error ", 
        - 10208060 => "The serviceIs has 1 more area ", 
        - 10208039 => "serviceId is <= 0 ", 
        - 10208042 => "areaid is < 0 ", 
        - 10208045 => "name is null ", 
        - 10208046 => "name is more than 50 chars ", 
        - 10208047 => "notes is more than 200 chars ", 
        - 10208048 => "The ServiceId is not Exist ", 
        - 10208049 => "The AreaId for the ServiceId is not Exist ", 
        - 10208050 => "update area error ", 
        - 10208051 => "serviceId is <= 0 ", 
        - 10208052 => "areaid is < 0 ", 
        - 10208057 => "The ServiceId is not Exist ", 
        - 10208058 => "The AreaId for the ServiceId is not Exist ", 
        - 10208059 => "delete area error "
    );
    /**
     * 计费服务添加应用
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $name	  #应用名称
     * @param string $env     #环境（测试|正式）
     * @param int $timetypeid #计费类型
     * @param int $spId		  #服务商ID，默认为1，盛大
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function addApp (&$message, $appId, $name, $env = 'pre', $timetypeid = 5, $spId = 1, $notes = '')
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['spId'] = $spId;
        $params['name'] = $name;
        $params['timetypeid'] = $timetypeid;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('addApp', $params, $wsdl['wsdl'], $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params), SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $errMsg[SDK_LOG_EXCEPTION] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::fatal($errMsg, __CLASS__);
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        if ($result->result !== "0") {
            $message = $result->message;
            if ($result->result == self::ADD_APP_EXISTS_CODE) {
                self::log($errMsg, __CLASS__);
                return true;
            }
            $method = isset(self::$errNo[$result->result]) ? "warn" : "error";
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::$method($errMsg, __CLASS__);
            self::setErrNo($result->result);
            return FALSE;
        }
        self::addArea($message, $appId, $env, 0, '0区');
        self::log($errMsg, __CLASS__, true);
        return true;
    }
    /**
     * 计费服务修改应用
     *
     * @param string $message #错误信息
     * @param int $appId  	  #APP ID	
     * @param string $name	  #应用名称
     * @param string $env     #环境（测试|正式）
     * @param int $timetypeid #计费类型
     * @param int $spId		  #服务商ID，默认为1，盛大
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function modifyApp (&$message, $appId, $name, $env = 'pre', $timetypeid = 5, $spId = 1, $notes = '')
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['spId'] = $spId;
        $params['name'] = $name;
        $params['timetypeid'] = $timetypeid;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('modifyApp', $params, $wsdl['wsdl'], $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params), SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $errMsg[SDK_LOG_EXCEPTION] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::fatal($errMsg, __CLASS__);
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        if ($result->result !== "0") {
            $method = isset(self::$errNo[$result->result]) ? "warn" : "error";
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::$method($errMsg, __CLASS__);
            $message = $result->message;
            self::setErrNo($result->result);
            return FALSE;
        }
        self::log($errMsg, __CLASS__, true);
        return true;
    }
    /**
     * 计费服务删除应用
     *
     * @param string $message #错误信息
     * @param int $appId  	  #APP ID	
     * @param string $name	  #应用名称
     * @param string $env     #环境（测试|正式）
     * @param int $timetypeid #计费类型
     * @param int $spId		  #服务商ID，默认为1，盛大
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function deleteApp (&$message, $appId, $name, $env = 'pre', $timetypeid = 5, $spId = 1, $notes = '')
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        self::deleteArea($message, $appId, $env, 0, '0区');
        $params['serviceId'] = $appId;
        $params['spId'] = $spId;
        $params['name'] = $name;
        $params['timetypeid'] = $timetypeid;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('deleteApp', $params, $wsdl['wsdl'], $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params), SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $errMsg[SDK_LOG_EXCEPTION] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::fatal($errMsg, __CLASS__);
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        if ($result->result !== "0") {
            $message = $result->message;
            if ($result->result == - 10208037) {
                self::log($errMsg, __CLASS__);
                return TRUE;
            }
            $method = isset(self::$errNo[$result->result]) ? "warn" : "error";
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::$method($errMsg, __CLASS__);
            self::setErrNo($result->result);
            return FALSE;
        }
        self::log($errMsg, __CLASS__, true);
        return true;
    }
    /**
     * 配置区组信息
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $env     #环境（测试|正式）
     * @param int $areaId	  #区ID
     * @param string $name    #区名称
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function addArea (&$message, $appId, $env = 'pre', $areaId = 1, $name = '默认', $notes = '')
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['areaId'] = $areaId;
        $params['name'] = $name;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('addArea', $params, $wsdl['wsdl'], $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params), SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $errMsg[SDK_LOG_EXCEPTION] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::fatal($errMsg, __CLASS__);
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        if ($result->result !== "0") {
            $message = $result->message;
            if ($result->result == self::ADD_AREA_EXISTS_CODE) {
                self::log($errMsg, __CLASS__);
                return true;
            }
            $method = isset(self::$errNo[$result->result]) ? "warn" : "error";
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::$method($errMsg, __CLASS__);
            self::setErrNo($result->result);
            return FALSE;
        }
        self::log($errMsg, __CLASS__, true);
        return true;
    }
    /**
     * 修改区组信息
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $env     #环境（测试|正式）
     * @param int $areaId	  #区ID
     * @param string $name    #区名称
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function modifyArea (&$message, $appId, $env = 'pre', $areaId = 1, $name = '默认', $notes = '')
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['areaId'] = $areaId;
        $params['name'] = $name;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('modifyArea', $params, $wsdl['wsdl'], $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params), SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $errMsg[SDK_LOG_EXCEPTION] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::fatal($errMsg, __CLASS__);
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        if ($result->result !== "0") {
            $method = isset(self::$errNo[$result->result]) ? "warn" : "error";
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::$method($errMsg, __CLASS__);
            $message = $result->message;
            self::setErrNo($result->result);
            return FALSE;
        }
        self::log($errMsg, __CLASS__, true);
        return true;
    }
    /**
     * 删除区组信息
     *
     * @param string $message #错误信息
     * @param int $appId      #APP ID	
     * @param string $env     #环境（测试|正式）
     * @param int $areaId	  #区ID
     * @param string $name    #区名称
     * @param string $notes	  #notes
     * @return Boolean
     */
    public static function deleteArea (&$message, $appId, $env = 'pre', $areaId = 1, $name = '默认', $notes = '')
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $params['serviceId'] = $appId;
        $params['areaId'] = $areaId;
        $params['name'] = $name;
        $params['notes'] = $notes;
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('deleteArea', $params, $wsdl['wsdl'], $options, $message);
        $errMsg = array(SDK_LOG_METHOD=> __FUNCTION__ , SDK_LOG_PARAMS=> json_encode($params), SDK_LOG_ENV=>$env);
        if ($result === FALSE) {
            $errMsg[SDK_LOG_EXCEPTION] = $message;
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::fatal($errMsg, __CLASS__);
            return FALSE;
        }
        $errMsg[SDK_LOG_REUTRN] = json_encode($result);
        if ($result->result !== "0") {
            if ($result->result == - 10208057 || $result->result == - 10208058) {
                self::log($errMsg, __CLASS__);
                return TRUE;
            }
            $method = isset(self::$errNo[$result->result]) ? "warn" : "error";
            $errMsg[SDK_LOG_LOCATION] = $wsdl['wsdl'];
            self::$method($errMsg, __CLASS__);
            $message = $result->message;
            self::setErrNo($result->result);
            return FALSE;
        }
        self::log($errMsg, __CLASS__, true);
        return true;
    }
    /**
     * 刷新计费缓存
     */
    public static function updateConfig (&$message, $env = 'pre')
    {
        $wsdl = $env === SDK_ENV_TEST ? sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL) : sdk_fetch_config(
        SDK_ENV . self::BILLING_WSDL);
        $options['soap_version'] = SOAP_1_1;
        if (isset($wsdl['options']) && $wsdl['options']) {
            $options = array_merge($options, $wsdl['options']);
        }
        $result = self::soapRequest('updateConfig', array(), $wsdl['wsdl'], $options, $message);
        if ($result === FALSE) {
            self::log(
            "method: " . __FUNCTION__ . "\tparams: " . json_encode($params) . "\tenv: {$env}\texception: " .
             $message, __CLASS__);
            return FALSE;
        }
        print_r($result);
    }
    /**
     * 自检函数
     */
    public static function selfTest ()
    {
        $sdk1 = sdk_fetch_config(SDK_ENV . self::PRE_BILLING_WSDL);
        $sdk2 = sdk_fetch_config(SDK_ENV . self::BILLING_WSDL);
        return self::curlTest(array(
            $sdk1['wsdl'], 
            $sdk2['wsdl']
        ));
    }
}
